package com.foreverwin.jzymes.qc.check.controller;

import com.foreverwin.jzymes.common.domain.NcData;
import com.foreverwin.jzymes.common.enums.TaskTypeEnum;
import com.foreverwin.jzymes.common.service.*;
import com.foreverwin.jzymes.common.util.CarrierUtil;
import com.foreverwin.jzymes.common.util.CommonMethods;
import com.foreverwin.jzymes.framework.annotation.ParamBind;
import com.foreverwin.jzymes.framework.base.AjaxResult;
import com.foreverwin.jzymes.framework.base.BaseController;
import com.foreverwin.jzymes.framework.exception.BusinessException;
import com.foreverwin.jzymes.framework.util.I18nUtil;
import com.foreverwin.jzymes.job.TaskService;
import com.foreverwin.jzymes.qc.check.domain.QcCheck;
import com.foreverwin.jzymes.qc.check.domain.QcCheckFaiFail;
import com.foreverwin.jzymes.qc.check.service.IQcCheckAdiviceService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/qcCheck")
public class QcCheckController extends BaseController {
    @Autowired
    IQcCheckAdiviceService qcCheckAdiviceService;
    @Autowired
    ICncReqLogAdviceService cncReqLogAdviceService;
    @Autowired
    IResourceAdviceService resourceAdviceService;
    @Autowired
    ICarrierBindAdviceService carrierBindAdviceService;
    @Autowired
    IItemAdviceService itemAdviceService;
    @Autowired
    ICustomFieldsAdviceService customFieldsAdviceService;
    @Autowired
    INCCodeAdviceService ncCodeAdviceService;
    @Autowired
    TaskService taskService;

    @RequestMapping("/submit")
    @ParamBind
    public AjaxResult submit( String taskType, QcCheck qcCheck, ArrayList<NcData> ncList, String faiId ){

        try{
            qcCheck.setSite( CommonMethods.getSite() );
            qcCheck.setCarrier( CarrierUtil.buildCarrier( qcCheck.getSite(), qcCheck.getCarrier() ) );
            qcCheckAdiviceService.submit( taskType, qcCheck, ncList, faiId );

            return AjaxResult.returnSuccess();
        }catch ( BusinessException e ) {
            return AjaxResult.returnError(e.getMessage());
        }
    }

    /**
     * 查询三次元检验任务信息
     *
     * @param carrier
     * @param taskType
     * @return
     */
    @RequestMapping("/findFaiQcCheckTask")
    public AjaxResult findFaiQcCheckTask( @RequestParam String carrier, @RequestParam String taskType ){

        try{
            String site = CommonMethods.getSite();
            carrier = CarrierUtil.buildCarrier( site, carrier );
            Map<String,Object> faiData = qcCheckAdiviceService.findFaiDataByCarrier( site, carrier );
            if( faiData == null ){
                throw BusinessException.build( "qc.check.carrierFaiDataNotExist", new String[]{ carrier } );
            }

            String resource = ((String) faiData.get( "SN" )).toUpperCase();
            String faiResource = (String) faiData.get( "EAP_ID" );
            String faiId = faiData.get( "SEQ" ).toString();

            Map<String,Object> resourceMap = resourceAdviceService.existResource( site, resource );

            QcCheck qcCheck = new QcCheck();
            qcCheck.setSite( site );
            qcCheck.setResrce( resource );
            qcCheck.setCarrier( carrier );
            qcCheck.setFaiResrce( faiResource );

            List<QcCheck> openQcCheckList = qcCheckAdiviceService.findOpenQcCheck( site, TaskTypeEnum.valueOf( taskType ), carrier, resource );
            if( openQcCheckList != null && openQcCheckList.size() > 0 ){
                qcCheck.setTaskId( openQcCheckList.get(0).getTaskId() );
            }

            List<QcCheckFaiFail> qcCheckFaiFailList = qcCheckAdiviceService.resolveFaiData( qcCheck, faiData );
            if( qcCheckFaiFailList.size() == 0 ){
                qcCheck.setCheckResult( "Y" );
            }else {
                qcCheck.setCheckResult( "N" );
            }

            List<Map<String,Object>> rtnNcDataList = qcCheckAdiviceService.buildFaiNcDataList( qcCheck.getSite(), qcCheck.getCarrier() );

            Map<String,Object> returnMap = new HashMap<>();
            returnMap.put( "resrce", resourceMap.get( "RESRCE" ) );
            returnMap.put( "description", resourceMap.get( "DESCRIPTION" ) );
            returnMap.put( "faiResrce", faiResource );
            returnMap.put( "taskId", qcCheck.getTaskId() );
            returnMap.put( "faiId", faiId );
            returnMap.put( "checkResult", qcCheck.getCheckResult() );
            returnMap.put( "qcCheckFaiFailList", qcCheckFaiFailList );
            returnMap.put( "ncDetailList", rtnNcDataList );

            return AjaxResult.returnSuccess().put( "value", returnMap );
        }catch ( BusinessException e ) {
            return AjaxResult.returnError(e.getMessage());
        }
    }

    /**
     * 查询本体FAI超过范围的量测数据
     *
     * @param carrier
     * @return
     */
    @RequestMapping("/findFAINCData")
    public AjaxResult findFAINCData( @RequestParam String carrier ){
        try{
            String site = CommonMethods.getSite();
            carrier = CarrierUtil.buildCarrier( site, carrier );

            List<Map<String,Object>> rtnNcDataList = qcCheckAdiviceService.buildFaiNcDataList( site, carrier );
            return AjaxResult.returnSuccess().put( "value", rtnNcDataList );
        }catch ( BusinessException e ) {
            return AjaxResult.returnError(e.getMessage());
        }
    }

    /**
     * 查询本体绑定设备
     *
     * @param carrier
     * @return
     */
    @RequestMapping("/carrier/resource")
    public AjaxResult findCarrierBindResource( @RequestParam String carrier ){
        String site = CommonMethods.getSite();
        carrier = CarrierUtil.buildCarrier( site, carrier );

        String resource = cncReqLogAdviceService.findCarrierBindResource( site, carrier );
        if( StringUtils.isBlank( resource ) ){
            return AjaxResult.returnSuccess().put( "carrier", carrier );
        }
        Map<String,Object> resourceMap = resourceAdviceService.existResource( site, resource );
        Map<String,Object> rtnResourceMap = new HashMap<>();
        rtnResourceMap.put( "resrce", resourceMap.get( "RESRCE" ) );
        rtnResourceMap.put( "description", resourceMap.get( "DESCRIPTION" ) );

        //return AjaxResult.returnSuccess().put( "value", rtnResourceMap ).put( "ncDetailList", carrierItemList );
        return AjaxResult.returnSuccess().put( "value", rtnResourceMap );
    }

    /**
     * 查询目检、巡检任务信息
     *
     * @param taskType
     * @param carrier
     * @param resrce
     * @return
     */
    @RequestMapping("/findOpenQcCheckTask")
    public AjaxResult findOpenQcCheckTask( @RequestParam String taskType, @RequestParam String carrier, @RequestParam String resrce ){
        String site = CommonMethods.getSite();
        Assert.hasText( carrier, I18nUtil.getI18nText( "common.carrierNotBlank" ) );
        Assert.hasText( taskType, I18nUtil.getI18nText( "common.taskTypeNotBlank" ) );
        Assert.hasText( resrce, I18nUtil.getI18nText( "common.resourceNotBlank" ) );

        carrier = CarrierUtil.buildCarrier( site, carrier );

        List<QcCheck> qcCheckList = null;
        qcCheckList = qcCheckAdiviceService.findOpenQcCheck( site, TaskTypeEnum.valueOf( taskType ), carrier, resrce );

        if( qcCheckList == null || qcCheckList.size() == 0 ){
            return AjaxResult.returnSuccess().put( "value", "" );
        }else {
            return AjaxResult.returnSuccess().put( "value", qcCheckList.get(0).getTaskId() );
        }
    }

    /**
     * 巡检抽检本体
     *
     * @param resrce
     * @return
     */
    @RequestMapping("/sampleCarrier")
    public AjaxResult sampleCarrier( @RequestParam String resrce ){
        try{
            String site = CommonMethods.getSite();
            Assert.hasText( resrce, I18nUtil.getI18nText( "common.resourceNotBlank" ) );

            if( !resourceAdviceService.isAutoLineResource( site, resrce ) ){
                throw BusinessException.build( "qc.check.resourceNotSendSampleCommand", new String[]{ resrce } );
            }
            taskService.sendQcCheckCommand( site, resrce );
            return AjaxResult.returnSuccess();

        }catch( BusinessException e ){
            return AjaxResult.returnError( e.getMessage() );
        }
    }
}
